package org.smarthings.things.automation.test.mock;

import java.io.File;
import java.io.InputStream;
import java.util.Dictionary;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.BundleListener;
import org.osgi.framework.Filter;
import org.osgi.framework.FrameworkListener;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.ComponentInstance;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;

@SuppressWarnings("rawtypes")
public class MockedComponentContext implements ComponentContext {

	private EventHandler handler;

	public void fireThingEvent(Event event) {
		if (handler != null) {
			System.out.println("MockedComponentContext.fireThingEvent : " + event);
			handler.handleEvent(event);
		}
	}

	@Override
	public void disableComponent(String arg0) {

	}

	@Override
	public void enableComponent(String arg0) {

	}

	@Override
	public BundleContext getBundleContext() {
		return new MockedBundleContext();
	}

	@Override
	public ComponentInstance getComponentInstance() {
		return null;
	}

	@Override
	public Dictionary getProperties() {
		return null;
	}

	@Override
	public ServiceReference getServiceReference() {
		return null;
	}

	@Override
	public Bundle getUsingBundle() {
		return null;
	}

	@Override
	public Object locateService(String arg0) {
		return null;
	}

	@Override
	public Object locateService(String arg0, ServiceReference arg1) {
		return null;
	}

	@Override
	public Object[] locateServices(String arg0) {
		return null;
	}

	private class MockedBundleContext implements BundleContext {

		@Override
		public void addBundleListener(BundleListener arg0) {

		}

		@Override
		public void addFrameworkListener(FrameworkListener arg0) {

		}

		@Override
		public void addServiceListener(ServiceListener arg0) {

		}

		@Override
		public void addServiceListener(ServiceListener arg0, String arg1)
				throws InvalidSyntaxException {

		}

		@Override
		public Filter createFilter(String arg0) throws InvalidSyntaxException {
			return null;
		}

		@Override
		public ServiceReference[] getAllServiceReferences(String arg0, String arg1)
				throws InvalidSyntaxException {
			return null;
		}

		@Override
		public Bundle getBundle() {
			return null;
		}

		@Override
		public Bundle getBundle(long arg0) {
			return null;
		}

		@Override
		public Bundle[] getBundles() {
			return null;
		}

		@Override
		public File getDataFile(String arg0) {
			return null;
		}

		@Override
		public String getProperty(String arg0) {
			return null;
		}

		@Override
		public Object getService(ServiceReference arg0) {
			return null;
		}

		@Override
		public ServiceReference getServiceReference(String arg0) {
			return null;
		}

		@Override
		public ServiceReference[] getServiceReferences(String arg0, String arg1)
				throws InvalidSyntaxException {
			return null;
		}

		@Override
		public Bundle installBundle(String arg0) throws BundleException {
			return null;
		}

		@Override
		public Bundle installBundle(String arg0, InputStream arg1) throws BundleException {
			return null;
		}

		@Override
		public ServiceRegistration registerService(String[] arg0, Object arg1, Dictionary arg2) {
			return null;
		}

		@Override
		public ServiceRegistration registerService(String arg0, Object arg1, Dictionary arg2) {
			if (arg1 instanceof EventHandler) {
				handler = (EventHandler) arg1;
				System.out
						.println("MockedComponentContext.MockedBundleContext.registerService() : "
								+ handler);
			}
			return null;
		}

		@Override
		public void removeBundleListener(BundleListener arg0) {

		}

		@Override
		public void removeFrameworkListener(FrameworkListener arg0) {

		}

		@Override
		public void removeServiceListener(ServiceListener arg0) {

		}

		@Override
		public boolean ungetService(ServiceReference arg0) {
			return false;
		}

	}

}
